<?php
class Controller_Admin_Stats extends Controller_Template
{
    public function __construct($request, $response)
    {
        parent::__construct($request, $response);
        $this->template = 'smarty:stats/admin/index';
    }
	 
    public function action_index()
    {
    	$day = date('Y-m-d',strtotime("-1 day"));
    	
    	//获取uvp统计数据
    	$upv_statis = Stats::upvstatis($day);
    	
    	//获取店铺统计数据
    	$shop = Stats::shopinfo($day,'shop');
    	
    	//获取内容数据
    	$content = Stats::shopinfo($day,'content');
    	
    	//获取订单数据
    	$order  = Stats::shopinfo($day,'order');
    	
    	$this->template->set('upv',$upv_statis);
    	$this->template->set('shop',$shop);
    	$this->template->set('content',$content);
    	$this->template->set('order',$order);
//     	exit();
    }
	
	public function action_jubao()
	{
		$day = date('Y-m-d',strtotime("-1 day"));
		$day = '2012-09-13';
		$data = Stats::jubao($day);
		var_dump($data);
		$this->template->set('jubao',$data);
		$this->template->set_filename('smarty:stats/admin/jubao');
	}
	
	/**
	 *  内容审核数据统计
	 *
	 */
	public function action_cuxiao_statis()
	{
		$day = date("Y-m-d",strtotime("-1 day"));
		$statis = Stats::cxinfo($day);
		$file  = "smarty:statis/admin/cuxiao";
		$this->template->set('statis',$statis);
		$this->template->set_filename($file);
	}
	/**
	 *  举报统计
	 *
	 */
	public function action_jubao_statis()
	{
		$day = date("Y-m-d",strtotime("-1 day"));
		$statis = Stats::jubao($day);
		$file  = "smarty:statis/admin/jubao";
		$this->template->set('statis',$statis);
		$this->template->set_filename($file);
	}
	/**
	 *  客户统计 
	 * 
	 */
	public function action_customer_statis()
	{
		$day    = date("Y-m-d",strtotime("-1 day"));
		$type   = array('shop','content','loupan','order');
		$statis = array();
		foreach($type as $value){
		 $statis[$value] = Stats::shopinfo($day,$value);
		}

		$file  = "smarty:statis/admin/customer";
		$this->template->set('statis',$statis);
		$this->template->set_filename($file);
	}
	
	/**
	 * 客户跟踪
	 */
	public function action_customer_behavior()
	{
		// 获取客户行为
		$free_shop_behavior = MUser_Action::factory('Stats_Shopbehavior')->summary();

		// 获取未付款客户列表
		$query  = array('shop_type' => array('free'),'order_by'=>array('apply_time'=>'desc'));
		$unpaid_shoplist = Shop::search($query,0,5);
		foreach ($unpaid_shoplist['list'] as & $item)
		{
			$item['apply_date'] = date('Y-m-d H:i',$item['apply_time']);
		}

		// 未付款订单
		$query = array('status'=>array('unpaid','timeout','removed'),'order_by'=>array('create_time'=>'desc'));

		$order_list = Order::search($query,0,5);
		foreach($order_list['list'] as & $item)
		{
			$item['shop_info'] = Shop::shop_info($item['shop_id']);
		}
		
		$this->template->set('unpaid_shoplist',$unpaid_shoplist);
		$this->template->set('order_list',$order_list);
		$this->template->set_filename('smarty:statis/admin/shopbehavior');
		$this->template->set('free_shop_behavior',$free_shop_behavior);
	}
	
	/**
	 * 店铺行为
	 */
	public function action_shop_behavior($page=1)
	{
		$action = MUser_Action::factory('Stats_Shopbehavior');
		$page   = max(1, (int) $page);
// 		$limit  = Kohana::config("muser.page_size.{$action_name}");
		if (empty($limit))
			$limit = 10;
		$offset = ($page - 1) * $limit;
		
		$this->template->set_filename('smarty:statis/admin/section/shop_behavior');
		
		$records = $action->records($offset, $limit);
		$count   = Arr::get($records, 'total', 0);
        $this->stats_build_page($limit,$count);
		
		$this->template->set('free_shop_behavior', $records);
	}

	/**
	 * 新申请店铺
	 *
	 */
	public function action_apply_new($page=1)
	{
		$query  = array('shop_type' => array('free'),'order_by'=>array('apply_time'=>'desc'));
		
		$limit = 10;
		$start                = $limit*($page-1);
	    if($start <= 0)$start = 0;

		$unpaid_shoplist = Shop::search($query,$start,$limit);
		$count = intval($unpaid_shoplist['count']);
        $this->stats_build_page($limit,$count);

		if($unpaid_shoplist['list']){
		foreach ($unpaid_shoplist['list'] as & $item)
		{
			$item['apply_date'] = date('Y-m-d H:i',$item['apply_time']);
		}
		}
	    $this->template->set('unpaid_shoplist',$unpaid_shoplist);
		$this->template->set_filename('smarty:statis/admin/section/shop_new');
	}
	/**
	 * 未付款订单
	 *
	 */
	public function action_order_no($page=1)
	{
	    $query = array('status'=>array('unpaid','timeout','removed'),'order_by'=>array('create_time'=>'desc'));
		
		$limit = 10;
		$start = $limit*($page-1);
	    if($start <= 0)$start = 0;

		$order_list = Order::search($query,$start,$limit);
		$count = intval($order_list['count']);
        $this->stats_build_page($limit,$count);
        
		if($order_list['list']){
		foreach($order_list['list'] as & $item)
		{
			$item['shop_info'] = Shop::shop_info($item['shop_id']);
		}
		}
		$this->template->set('order_list',$order_list);
		$this->template->set_filename('smarty:statis/admin/section/order_nopay');
	}

	/**
	 * 统计分页效果
	 */
	private function stats_build_page($limit,$count)
	{
	  $page_total           = ceil($count/$limit);
	  $page_config          = array(
			  'current_page'     => array('source'=> 'route','key'=>'param1'),
			  'total_items'      => $count,
			  'items_per_page'   => $limit,
			  // 'view'             => 'pagination/basic',
			   'auto_hide'        => true,
			   'first_page_in_url'=> true,
			  );
	  $pagination = Pagination::factory($page_config);
	  $this->template->set('pagination',$pagination->render());
	}
}
